/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function(a, b) {
  let result = "", next = false;
  if(a == "0" && b == "0")
    return "0";
  a = a.split('').reverse().join('');
  b = b.split('').reverse().join('');
  for(let i = 0; i < a.length && i < b.length; i++) {
    if(next) {
      if(a[i] == b[i] && a[i] == 0) {
        next = false;
        result += "1";
      } else {
        next = true;
        if(a[i] == b[i])
          result += "1";
        else
          result += "0";
      }
    } else {
      if(a[i] == b[i]) {
        if(a[i] == 1) next = true;
        result += "0";
      } else {
        result += "1";
      }
    }
  }
  if(a.length > b.length) {
    for(let i = b.length; i < a.length; i++) {
      if(next) {
        if(a[i] == 0) {
          next = false;
          result += '1';
        } else {
          next = true;
          result += '0';
        }
      } else {
        result += a[i];
        next = false;
      }
    }
  } else {
    for(let i = a.length; i < b.length; i++) {
      if(next) {
        if(b[i] == 0) {
          next = false;
          result += '1';
        } else {
          next = true;
          result += '0';
        }
      } else {
        result += b[i];
        next = false;
      }
    }
  }
  if(next) {
    result += "1";
  }

  return result.split('').reverse().join('');
};
let a = "11", b = "1";
console.log(addBinary(a, b));
a = "1010", b = "1011";
console.log(addBinary(a, b));
a = "0", b = "0";
console.log(addBinary(a, b));
a = "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101",
b = "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011";
console.log(addBinary(a, b));
